package com.mtoanmy.sqllite;


import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHandler extends SQLiteOpenHelper {

	public static final int DATABASE_VERSION = 1;
	public static final String DATABASE_NAME = "hueca_db";
	//public static final String TABLE_RECORD_DATA = "data";
	public static final String TABLE_RECORD_USER = "user";

	// Columns in userinfo table
	public static final String USER_TOKEN = "token";
	public static final String USER_ID = "user_id";

	// Chuá»—i truy váº¥n Ä‘á»ƒ táº¡o báº£ng
	//public static final String CREATE_TABLE_RECORD_DATA = "CREATE  TABLE data('date' TEXT NOT NULL,'percent' TEXT NOT NULL,'data_ao' TEXT NOT NULL,'result_ao' TEXT NOT NULL,'data_balo' TEXT NOT NULL,'result_balo' TEXT NOT NULL,'data_sotay' TEXT NOT NULL,'result_sotay' TEXT NOT NULL,'data_but' TEXT NOT NULL,'result_but' TEXT NOT NULL,'data_mockhoa' TEXT NOT NULL,'result_mockhoa' TEXT NOT NULL)";
	public static final String CREATE_TABLE_RECORD_USER = "CREATE  TABLE user('user_id' TEXT NOT NULL,'fullname' TEXT NOT NULL,'phone' TEXT NOT NULL,'email' TEXT NOT NULL,'key' TEXT NOT NULL,'city' TEXT NOT NULL)";
	
	public DBHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		//db.execSQL(CREATE_TABLE_RECORD_DATA);
		db.execSQL(CREATE_TABLE_RECORD_USER);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORD_DATA);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORD_USER);
		onCreate(db);

	}
	
/*	public List<String> searchOpt(String txt)
	{
		List<String> data = new ArrayList<String>();
		try {
			SQLiteDatabase db = this.getReadableDatabase();
			String query = "SELECT title FROM " + TABLE_OLD_SEARCH + " WHERE title LIKE '" + txt +"%'";
			Cursor cursor = db.rawQuery(query, null);
			while (cursor.moveToNext()) {
				String title = cursor.getString(0);
				data.add(title);

			}
			cursor.close();
		} catch (Exception e) {
			Log.v("DB", String.valueOf(e));
		}
		close();
		return data;
	}*/

	public void deleteAllRecordUsers() {
		SQLiteDatabase db = this.getWritableDatabase();
		String sql = "delete from " + TABLE_RECORD_USER;
		db.execSQL(sql);
		close();
	}
	
	/*public void deleteItemsRecordData(ObjRecordData data) {
		SQLiteDatabase db = this.getWritableDatabase();
		String sql = "delete from " + TABLE_RECORD_DATA+ " WHERE date = '"+data.getDate()+"'";
		db.execSQL(sql);
		close();
	}*/
	
	/*public void deleteItemsRecordUsers(ObjectUserInfo data) {
		SQLiteDatabase db = this.getWritableDatabase();
		String sql = "delete from " + TABLE_RECORD_USER
		+ " WHERE user_id = '"+data.getId()+"' AND fullname = '"+data.getFullName()+"' AND phone = '"+data.getPhone()+"' AND email ='"+data.getEmail()+"' AND key = '"+data.getEmail()+"'"+"' AND city = '"+data.getCity()+"'";
		db.execSQL(sql);
		close();
	}*/
	
	/*public boolean insertItemsRecordUser(ObjectUserInfo data) {
		try {
			SQLiteDatabase db = this.getWritableDatabase();

			ContentValues contentValue = new ContentValues();
			contentValue.put("user_id", data.getId());			
			contentValue.put("fullname", data.getFullName());			
			contentValue.put("phone", data.getPhone());			
			contentValue.put("email", data.getEmail());			
			contentValue.put("key", data.getKey());		
			contentValue.put("city", data.getCity());
			
			if (db.insert(TABLE_RECORD_USER, null, contentValue) == -1) {
				close();
				return false;
			} else
				return true;

		} catch (Exception e) {
		} finally {
			close();
			return false;
		}
	}*/
	
	/*public void deleteAllItemsRecordData() {
		SQLiteDatabase db = this.getWritableDatabase();
		String sql = "delete from " + TABLE_RECORD_DATA;
		db.execSQL(sql);
		close();
	}*/
	
	/*public List<ObjectUserInfo> getListUserInfo() {
		List<ObjectUserInfo> data = new ArrayList<ObjectUserInfo>();
		try {
			SQLiteDatabase db = this.getReadableDatabase();
			String query = "SELECT * FROM " + TABLE_RECORD_USER;
			Cursor cursor = db.rawQuery(query, null);
			while (cursor.moveToNext()) {
				String user_id = cursor.getString(0);
				String fullname = cursor.getString(1);
				String phone = cursor.getString(2);
				String email = cursor.getString(3);
				String key = cursor.getString(4);
				String city = cursor.getString(5);
				
				ObjectUserInfo obj = new ObjectUserInfo(user_id, email, fullname, phone, key,city);
				data.add(obj);
			}
			cursor.close();
		} catch (Exception e) {

		}
		close();
		return data;
	}
	
	public ObjectUserInfo getCurrentUserInfo() {
		ObjectUserInfo data = null;
		try {
			SQLiteDatabase db = this.getReadableDatabase();
			String query = "SELECT * FROM " + TABLE_RECORD_USER + " LIMIT 0,1";
			Cursor cursor = db.rawQuery(query, null);
			while (cursor.moveToNext()) {
				String user_id = cursor.getString(0);
				String fullname = cursor.getString(1);
				String phone = cursor.getString(2);
				String email = cursor.getString(3);
				String key = cursor.getString(4);
				String city = cursor.getString(5);
				
				data = new ObjectUserInfo(user_id, email, fullname, phone, key,city);
				
			}
			cursor.close();
		} catch (Exception e) {

		}
		close();
		return data;
	}*/
	
	/*public List<ObjRecordData> getAllItemsRecordData() {
		List<ObjRecordData> data = new ArrayList<ObjRecordData>();
		try {
			SQLiteDatabase db = this.getReadableDatabase();
			String query = "SELECT * FROM " + TABLE_RECORD_DATA;

			Cursor cursor = db.rawQuery(query, null);

			while (cursor.moveToNext()) {
//				String id = cursor.getString(0);
				String date = cursor.getString(0);
				String percent = cursor.getString(1);
				String data_ao = cursor.getString(2);
				String result_ao = cursor.getString(3);
				String data_balo = cursor.getString(4);
				String result_balo = cursor.getString(5);
				String data_sotay = cursor.getString(6);
				String result_sotay = cursor.getString(7);
				String data_but = cursor.getString(8);
				String result_but = cursor.getString(9);
				String data_mockhoa = cursor.getString(10);
				String result_mockhoa = cursor.getString(11);
				
				ObjRecordData user = new ObjRecordData(date, percent, data_ao, result_ao, data_balo, result_balo, data_sotay, result_sotay, data_but, result_but, data_mockhoa, result_mockhoa);
				data.add(user);
			}
			cursor.close();
		} catch (Exception e) {
		}
		close();
		return data;
	}*/
	
	/*public ObjRecordData getRecordData(String datetime) {
		try {
			SQLiteDatabase db = this.getReadableDatabase();
			String query = "SELECT * FROM " + TABLE_RECORD_DATA
					+ " WHERE date = '"+datetime+"'";

			Cursor cursor = db.rawQuery(query, null);

			while (cursor.moveToNext()) {
//				String id = cursor.getString(0);
				String date = cursor.getString(0);
				String percent = cursor.getString(1);
				String data_ao = cursor.getString(2);
				String result_ao = cursor.getString(3);
				String data_balo = cursor.getString(4);
				String result_balo = cursor.getString(5);
				String data_sotay = cursor.getString(6);
				String result_sotay = cursor.getString(7);
				String data_but = cursor.getString(8);
				String result_but = cursor.getString(9);
				String data_mockhoa = cursor.getString(10);
				String result_mockhoa = cursor.getString(11);
				ObjRecordData user = new ObjRecordData(date, percent, data_ao, result_ao, data_balo, result_balo, data_sotay, result_sotay, data_but, result_but, data_mockhoa, result_mockhoa);
				cursor.close();
				close();
				return user;

			}
			// cursor.close();
		} catch (Exception e) {
		}
		close();
		return null;
	}*/

	/*public boolean insertRecordData(ObjRecordData user) {
		try {
			SQLiteDatabase db = this.getWritableDatabase();

			ContentValues contentValue = new ContentValues();

//			contentValue.put("id", user.getId());
			contentValue.put("date", user.getDate());
			contentValue.put("percent", user.getPercent());
			contentValue.put("data_ao", user.getDataAo());
			contentValue.put("result_ao", user.getResultAo());
			contentValue.put("data_balo", user.getDataBalo());
			contentValue.put("result_balo", user.getResultBalo());
			contentValue.put("data_sotay", user.getDataSotay());
			contentValue.put("result_sotay", user.getResultSotay());
			contentValue.put("data_but", user.getDataBut());
			contentValue.put("result_but", user.getResultBut());
			contentValue.put("data_mockhoa", user.getDataMockhoa());
			contentValue.put("result_mockhoa", user.getResultMockhoa());

			if (db.insert(TABLE_RECORD_DATA, null, contentValue) == -1) {
				close();
				return false;
			} else
				return true;

		} catch (Exception e) {
		} finally {
			close();
			return false;
		}
	}*/

	/*public void updateRecordUserInfo(ObjectUserInfo user) {
		try {
			SQLiteDatabase db = this.getWritableDatabase();

			ContentValues contentValue = new ContentValues();

//			contentValue.put("id", user.getId());
			contentValue.put("user_id", user.getId());
			contentValue.put("fullname", user.getFullName());
			contentValue.put("phone", user.getPhone());
			contentValue.put("email", user.getEmail());
			contentValue.put("key", user.getKey());
			contentValue.put("city", user.getCity());

			db.update(TABLE_RECORD_DATA, contentValue,
					"date = " + user.getDate(), null);
			db.update(TABLE_RECORD_USER, contentValue,
					"date = '"+user.getId()+"'", null);
			db.close();

		} catch (Exception e) {
		} finally {
			close();
		}
	}*/
}
